﻿package mortal.game.view.group.panel
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import frEngine.render.layer.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.common.*;
    import mortal.game.view.group.cellRenderer.*;
    import mortal.mvc.core.*;

    public class TeamInfoPanel extends GSprite
    {
        private var _bg:ScaleBitmap;
        private var _temaMateList:GTileList;
        private var _changeCaptainBtn:GButton;
        private var _disbandTeamBtn:GButton;
        private var _leaveTeamBtn:GButton;
        private var _teamName:GTextInput;
        private var _btnBox:GSprite;
        private var _memberInvite:GCheckBox;
        private var _autoEnter:GCheckBox;
        protected var _rect3d:Rect3DObject;
        protected var _window:Window;
        private var playerVec:Vector.<UserModelPlayer>;
        private var img2Vec:Vector.<Img2D>;
        private var _img2d:Img2D;

        public function TeamInfoPanel(param1:Window)
        {
            this.playerVec = new Vector.<UserModelPlayer>;
            this.img2Vec = new Vector.<Img2D>;
            this._window = param1;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.bg(0, 0, 773, 346, null);
            this._btnBox = UICompomentPool.getUICompoment(GSprite);
            this._btnBox.createDisposedChildren();
            this._btnBox.x = 167;
            this._btnBox.y = 396;
            this._window.contentTopOf3DSprite.addChild(this._btnBox);
            this._btnBox.configEventListener(MouseEvent.CLICK, this.operHandler);
            this._temaMateList = UIFactory.tileList(20, 93, 765, 310, this._window.contentTopOf3DSprite);
            this._temaMateList.rowHeight = 290;
            this._temaMateList.columnWidth = 153;
            this._temaMateList.horizontalGap = 0;
            this._temaMateList.verticalGap = 0;
            this._temaMateList.setStyle("cellRenderer", TeamMateCellRenderer);
            this._window.contentTopOf3DSprite.addChild(this._temaMateList);
            this._changeCaptainBtn = UIFactory.gButton(Language.getString(30202), 100, 0, 65, 22, this._btnBox);
            this._disbandTeamBtn = UIFactory.gButton(Language.getString(30203), 200, 0, 65, 22, this._btnBox);
            this._leaveTeamBtn = UIFactory.gButton(Language.getString(30204), 300, 0, 65, 22, this._btnBox);
            this._teamName = UIFactory.gTextInput(80, 64, 120, 22, this);
            this._teamName.maxChars = 7;
            this._teamName.restrict = "一-龥 -~·-=【】；‘、，。、.~！@#￥%……&*（）——+{}：’“”|《》？/*.‘’";
            this._teamName.configEventListener(FocusEvent.FOCUS_OUT, this.changeTeamName);
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(30205), 20, 68, 80, 20, this, GlobalStyle.textFormatBai));
            this._memberInvite = UIFactory.checkBox(Language.getString(30207), 625, 384, 130, 28, this._window.contentTopOf3DSprite);
            this._memberInvite.configEventListener(Event.CHANGE, this.canInviteSelectedChanged);
            this._autoEnter = UIFactory.checkBox(Language.getString(30208), 625, 404, 130, 28, this._window.contentTopOf3DSprite);
            this._autoEnter.configEventListener(Event.CHANGE, this.canJoinFreeSelectedChanged);
            this.add3DRole();
            this.updateTeamMate();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._btnBox.dispose(param1);
            this._temaMateList.dispose(param1);
            this._changeCaptainBtn.dispose(param1);
            this._disbandTeamBtn.dispose(param1);
            this._leaveTeamBtn.dispose(param1);
            this._teamName.dispose(param1);
            this._memberInvite.dispose(param1);
            this._autoEnter.dispose(param1);
            this._bg.dispose(param1);
            this._btnBox = null;
            this._temaMateList = null;
            this._changeCaptainBtn = null;
            this._disbandTeamBtn = null;
            this._leaveTeamBtn = null;
            this._teamName = null;
            this._memberInvite = null;
            this._autoEnter = null;
            this._bg = null;
            this.clear3d();
            if (this._img2d)
            {
                this._img2d.dispose(param1);
                this._img2d = null;
            }
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            this.playerVec.length = 0;
            this.img2Vec.length = 0;
            super.disposeImpl(param1);
            return;
        }// end function

        private function getDataProvider() : DataProvider
        {
            var _loc_3:int = 0;
            var _loc_4:SGroupPlayer = null;
            var _loc_5:Object = null;
            var _loc_1:* = new DataProvider();
            var _loc_2:* = Cache.instance.group.players;
            this.clear3d();
            while (_loc_3 < 5)
            {
                
                if (!Cache.instance.group.isInGroup && _loc_3 == 0)
                {
                    _loc_4 = Cache.instance.group.selfInfo;
                }
                else
                {
                    _loc_4 = _loc_2[_loc_3] ? (_loc_2[_loc_3]) : (null);
                }
                this.update3DRoleModel(_loc_4, _loc_3);
                _loc_5 = new Object();
                _loc_5.data = _loc_4;
                _loc_5.index = _loc_3;
                _loc_1.addItem(_loc_5);
                _loc_3++;
            }
            return _loc_1;
        }// end function

        private function operHandler(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Object = null;
            if (event.target == this._disbandTeamBtn)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GroupDisbanGroup));
            }
            else if (event.target == this._leaveTeamBtn)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GroupLeave));
            }
            else if (event.target == this._changeCaptainBtn)
            {
                _loc_2 = this._temaMateList.selectedIndex;
                if (_loc_2 >= 0)
                {
                    _loc_3 = this._temaMateList.getItemAt(_loc_2);
                    if (_loc_3.data)
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.GroupModifyCaptain, (_loc_3.data as SGroupPlayer).player.entityId));
                        return;
                    }
                }
                MsgManager.showRollTipsMsg(Language.getString(30248));
            }
            return;
        }// end function

        private function getSetting() : SGroupSetting
        {
            var _loc_1:* = new SGroupSetting();
            _loc_1.groupId = new SEntityId();
            _loc_1.name = this._teamName.text;
            _loc_1.memberInvite = this._memberInvite.selected;
            _loc_1.autoEnter = this._autoEnter.selected;
            return _loc_1;
        }// end function

        private function changeTeamName(event:FocusEvent) : void
        {
            if (this._teamName.text == Cache.instance.group.groupName)
            {
                return;
            }
            if (Cache.instance.role.entityInfo.level < GameDefConfig.instance.getSystemOpenLevel(90))
            {
                this._teamName.text = Cache.instance.group.groupName;
                return MsgManager.showRollTipsMsg(Language.getString(35305), MsgRollTipsType.msgRollTips3);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupSetting, this.getSetting()));
            return;
        }// end function

        private function canInviteSelectedChanged(event:Event) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupSetting, this.getSetting()));
            return;
        }// end function

        private function canJoinFreeSelectedChanged(event:Event) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupSetting, this.getSetting()));
            return;
        }// end function

        private function resetBtnPos() : void
        {
            if (Cache.instance.group.isInGroup)
            {
                this._btnBox.visible = true;
                if (Cache.instance.group.isCaptain)
                {
                    this._changeCaptainBtn.visible = true;
                    this._disbandTeamBtn.visible = true;
                    this._btnBox.x = 168;
                }
                else
                {
                    this._changeCaptainBtn.visible = false;
                    this._disbandTeamBtn.visible = false;
                    this._btnBox.x = 70;
                }
            }
            else
            {
                this._btnBox.visible = false;
            }
            return;
        }// end function

        public function updateTeamMate() : void
        {
            this._temaMateList.dataProvider = this.getDataProvider();
            this._temaMateList.selectedIndex = -1;
            this._temaMateList.drawNow();
            this.resetBtnPos();
            this._memberInvite.selected = Cache.instance.group.memberInvite;
            this._autoEnter.selected = Cache.instance.group.autoEnter;
            if (Cache.instance.group.isInGroup)
            {
                this._teamName.text = Cache.instance.group.groupName;
                if (Cache.instance.group.isCaptain)
                {
                    this._teamName.editable = true;
                    this._memberInvite.mouseEnabled = true;
                    this._autoEnter.mouseEnabled = true;
                }
                else
                {
                    this._teamName.editable = false;
                    this._memberInvite.mouseEnabled = false;
                    this._autoEnter.mouseEnabled = false;
                }
            }
            else
            {
                this._teamName.text = "";
                this._teamName.editable = false;
                this._memberInvite.mouseEnabled = false;
                this._autoEnter.mouseEnabled = false;
            }
            return;
        }// end function

        public function updateSetting() : void
        {
            this._memberInvite.selected = Cache.instance.group.memberInvite;
            this._autoEnter.selected = Cache.instance.group.autoEnter;
            this._teamName.text = Cache.instance.group.groupName;
            return;
        }// end function

        protected function add3DRole() : void
        {
            this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(17, 90, 773, 346), this._window);
            Rect3DManager.instance.windowShowHander(null, this._window);
            this._rect3d.removeImg(this._img2d);
            this._img2d = new Img2D(null, this._bg.bitmapData, new Rectangle(0, 0, 773, 346), false);
            this._rect3d.addImg(this._img2d, Layer3DManager.backGroudImgLayer);
            return;
        }// end function

        protected function update3DRoleModel(param1:SGroupPlayer, param2:int) : void
        {
            var _loc_3:BitmapData = null;
            var _loc_4:Img2D = null;
            var _loc_5:UserModelPlayer = null;
            var _loc_6:EntityInfo = null;
            var _loc_7:TBoss = null;
            var _loc_8:TRobot = null;
            var _loc_9:Object = null;
            var _loc_10:Array = null;
            var _loc_11:int = 0;
            var _loc_12:int = 0;
            var _loc_13:ItemEquipInfo = null;
            var _loc_14:Object = null;
            if (this._rect3d)
            {
                if (param1)
                {
                    _loc_6 = param1.player.entityId == null ? (Cache.instance.role.roleEntityInfo) : (Cache.instance.entity.getEntityInfoById(param1.player.entityId));
                    _loc_5 = FrObjectPool.getObject(RoleModelPlayer, this);
                    if (_loc_5)
                    {
                        if (_loc_6)
                        {
                            _loc_7 = BossConfig.instance.getInfoByCode(_loc_6.entityInfo.code);
                            if (_loc_7)
                            {
                                _loc_8 = RobotConfig.instance.getRobotByCode(_loc_7.code);
                                _loc_9 = JSON.parse(_loc_8.extendStr);
                                _loc_10 = _loc_9.equip;
                                for each (_loc_14 in _loc_10)
                                {
                                    
                                    _loc_13 = ItemConfig.instance.getConfig(_loc_14.code) as ItemEquipInfo;
                                    if (_loc_13.type == EEquip._EEquipWeaponFashion)
                                    {
                                        _loc_12 = int(_loc_13.modelId);
                                        continue;
                                    }
                                    if (_loc_11 == 0 && _loc_13.type == EEquip._EEquipClothes)
                                    {
                                        _loc_11 = int(_loc_13.modelId);
                                        continue;
                                    }
                                    if (_loc_12 == 0 && _loc_13.type == EEquip._EEquipWeapon)
                                    {
                                        _loc_12 = int(_loc_13.modelId);
                                    }
                                }
                                if (_loc_6.entityInfo.level >= GameDefConfig.instance.getSystemOpenLevel(63))
                                {
                                    _loc_11 = MountConfig.instance.getFashionInfoByCode(EAdvanceComponentType._EAdvanceComponentTypeClothes, 1).modelId;
                                    _loc_12 = MountConfig.instance.getFashionInfoByCode(EAdvanceComponentType._EAdvanceComponentTypeWeapon, 1).modelId;
                                }
                                _loc_5.updateInfo(_loc_6.entityInfo.career, _loc_6.entityInfo.sex, _loc_11, _loc_12, _loc_6.hair, _loc_6.wing, _loc_6.entityInfo.VIP, _loc_6.isHideWing);
                            }
                            else
                            {
                                _loc_5.updateInfo(_loc_6.entityInfo.career, _loc_6.entityInfo.sex, _loc_6.clothes, _loc_6.weapon, _loc_6.hair, _loc_6.wing, _loc_6.entityInfo.VIP, _loc_6.isHideWing);
                            }
                            _loc_5.scaleAll = 1.4;
                            _loc_5.setRenderList(this._rect3d.renderList);
                            _loc_5.rotationY = 0;
                            this._rect3d.addObject3d(_loc_5, param2 * 153 + 76, 240);
                            this.playerVec.push(_loc_5);
                        }
                        _loc_4 = new Img2D(null, GlobalClass.getBitmapData(ImagesConst.GroupInBg), new Rectangle(0, 0, 150, 287), false);
                        _loc_4.x = param2 * 153 + 5;
                        _loc_4.y = 5;
                        this._rect3d.addImg(_loc_4, Layer3DManager.BackGroudEffectLayer);
                        this.img2Vec.push(_loc_4);
                    }
                }
                else
                {
                    _loc_4 = new Img2D(null, GlobalClass.getBitmapData(ImagesConst.GroupWaitBg), new Rectangle(0, 0, 150, 287), false);
                    _loc_4.x = param2 * 153 + 5;
                    _loc_4.y = 5;
                    this._rect3d.addImg(_loc_4, Layer3DManager.BackGroudEffectLayer);
                    this.img2Vec.push(_loc_4);
                }
            }
            return;
        }// end function

        private function clear3d() : void
        {
            var _loc_1:UserModelPlayer = null;
            var _loc_2:Img2D = null;
            for each (_loc_1 in this.playerVec)
            {
                
                this._rect3d.disposeObj3d(_loc_1);
            }
            this.playerVec.length = 0;
            for each (_loc_2 in this.img2Vec)
            {
                
                this._rect3d.removeImg(_loc_2);
            }
            this.img2Vec.length = 0;
            return;
        }// end function

    }
}
